package com.power.controller;

import com.power.domain.Category;
import com.power.model.Result;
import com.power.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 商品类目控制层
 * @Author dingchunlin
 * @Date 2024/07/13 23:08
 * @Version 1.0.0
 */
@Api(tags = "商品类目接口管理")
@RestController
@RequestMapping("/prod/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 获取所有商品类目
     * @return Result<List<Category>>
     */
    @ApiOperation("获取所有商品类目")
    @GetMapping("/table")
    @PreAuthorize("hasAnyAuthority('prod:category:page')")
    public Result<List<Category>> loadAllCategoryList() {
        List<Category> categoryList = categoryService.queryAllCategoryList();
        return Result.success(categoryList);
    }

    /**
     * 查询系统商品一级类目
     * @return Result<List<Category>>
     */
    @ApiOperation("查询系统商品一级类目")
    @GetMapping("/listCategory")
    @PreAuthorize("hasAnyAuthority('prod:category:page')")
    public Result<List<Category>> loadFirstCategoryList() {
        List<Category> categoryList = categoryService.queryFirstCategoryList();
        return Result.success(categoryList);
    }


    /**
     * 新增商品类目
     * @param category 商品类目实体类
     * @return Result<String>
     */
    @ApiOperation("新增商品类目")
    @PostMapping
    @PreAuthorize("hasAnyAuthority('prod:category:save')")
    public Result<String> saveCategory(@RequestBody Category category) {
        Boolean save = categoryService.saveCategory(category);
        return Result.handle(save);
    }

    /**
     * 根据标识查询商品类目信息
     * @param categoryId 商品类目ID
     * @return Result<Category>
     */
    @ApiOperation("根据标识查询商品类目信息")
    @GetMapping("/info/{categoryId}")
    @PreAuthorize("hasAnyAuthority('prod:category:info')")
    public Result<Category> loadCategoryInfo(@PathVariable("categoryId") Long categoryId) {
        Category category = categoryService.getById(categoryId);
        return Result.success(category);
    }

    /**
     * 修改商品类目信息
     * @param category 商品类目实体类
     * @return Result<String>
     */
    @ApiOperation("修改商品类目信息")
    @PutMapping
    @PreAuthorize("hasAnyAuthority('prod:category:update')")
    public Result<String> modifyCategory(@RequestBody Category category) {
        Boolean update = categoryService.modifyCategory(category);
        return Result.handle(update);
    }

    /**
     * 删除商品类目信息
     * @param categoryId 商品类目ID
     * @return Result<String>
     */
    @ApiOperation("删除商品类目信息")
    @DeleteMapping("/{categoryId}")
    @PreAuthorize("hasAnyAuthority('prod:category:delete')")
    public Result<String> removeCategory(@PathVariable("categoryId") Long categoryId) {
        Boolean removed = categoryService.removeCategoryById(categoryId);
        return Result.handle(removed);
    }

    /**
     * 微信小程序端获取商品类目
     * @param parentId 父类目ID
     * @return Result<List<Category>>
     */
    @ApiOperation(value = "微信小程序端获取商品类目")
    @GetMapping("/category/list")
    public Result<List<Category>> loadCategoryList(@RequestParam Long parentId) {
        List<Category> categoryList = categoryService.queryCategoryList(parentId);
        return Result.success(categoryList);
    }

    /**
     * 根据父类目ID查询子类目
     * @param parentId 父类目ID
     * @return Result<List<Category>>
     */
    @ApiOperation(value = "根据父类目ID查询子类目")
    @GetMapping("/getCategoryListByParentId")
    public Result<List<Category>> getCategoryListByParentId(@RequestParam Long parentId){
        //根据父类目ID查询子类目
        List<Category> categoryList = categoryService.queryCategoryListByParentId(parentId);
        return Result.success(categoryList);
    }
}
